package com.zhong.gang.pda.db.loction;

import android.content.Context;
import android.util.Log;

import com.zhong.gang.pda.ui.action.HuoBean;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class LocationMessageUtils {
    private static LocationMessageUtils locationMessageUtils;
    private static LoctionMessageDao loctionMessageDao;

    public LocationMessageUtils(Context context) {
        loctionMessageDao = new LoctionMessageDao(context);
    }

    public static LocationMessageUtils getIntent(Context context) {
        if (locationMessageUtils == null || loctionMessageDao == null) {
            locationMessageUtils = new LocationMessageUtils(context);
        }
        return locationMessageUtils;
    }

    public LoctionMessageDao getLoctionMessageDao() {
        return loctionMessageDao;
    }

    /**
     * 保存数据
     */
    public void saveData(HuoBean huoBean) {
        if (huoBean == null){
            return;
        }
        List<LoctionMessage> data = null;
        try {
            data = getLoctionMessageDao().getQueryBuilder()
                    .where().eq("id",huoBean.getId()).query();
            for (int n = 0 ;n < data.size();n++){
                if (data.get(n).getId() == huoBean.getId()){
                    data.get(n).setId(huoBean.getId());
                    data.get(n).setData(huoBean);
                    getLoctionMessageDao().updateData(data.get(n));
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LoctionMessage message = new LoctionMessage();
        message.setId(huoBean.getId());
        message.setData(huoBean);
        getLoctionMessageDao().save(message);
    }


    public HuoBean getData(HuoBean huoBean){
        List<LoctionMessage> data = null;
        try {
            data = getLoctionMessageDao().getQueryBuilder()
                    .where().eq("id",huoBean.getId()).query();
            for (int n = 0 ;n < data.size();n++){
                if (data.get(n).getId() == huoBean.getId()){
                    return data.get(n).getHuoBean();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public HuoBean getData(int id){
        List<LoctionMessage> data = null;
        try {
            data = getLoctionMessageDao().getQueryBuilder()
                    .where().eq("id",id).query();
            for (int n = 0 ;n < data.size();n++){
                if (data.get(n).getId() == id){
                    return data.get(n).getHuoBean();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delet(HuoBean huoBean){
        for (LoctionMessage message:getList()){
            if (message.getId() == huoBean.getId()){
                try {
                    getLoctionMessageDao().delectData(message);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                Log.e("==``",getLoctionMessageDao().queryAllData()+"----");
                return;
            }
        }
    }


    public void deletAll(){
       List<LoctionMessage> data =  getLoctionMessageDao().queryAllData();
        for (int n = 0 ;n <data.size();n++){
            try {
                getLoctionMessageDao().delectData(data.get(n));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List<LoctionMessage> getList(){
        List<LoctionMessage> data = getLoctionMessageDao().queryAllData();
        if (data == null)
            return new ArrayList<LoctionMessage>();
        return data;
    }
}
